package cn.edu.cimv.controller;

import cn.edu.cimv.constant.InfoConstant;
import cn.edu.cimv.pojo.Message;
import cn.edu.cimv.dao.entity.News;
import cn.edu.cimv.service.NewsService;
import cn.edu.cimv.util.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

/**
 * Created by 门铎 on 2016/8/1.
 */
@Controller
@RequestMapping("/cimv/article")
public class ArticleController {
    @Autowired
    private NewsService newsService;
    @Autowired
    private News news;
    @Autowired
    private Message message;
    @RequestMapping("list/{flag}")
    public String list(Model model,@PathVariable("flag")int flag){
        model.addAttribute("newsList",newsService.queryNews(flag));
        //0 是学术动态 1 是通知公告
        if(flag==0){
            return "article/list";
        }else{
            return "notices/list";
        }

    }
    @RequestMapping("new/{flag}")
    public String articleNew(@PathVariable("flag")int flag){
        if(flag==0){
            return "article/new";
        }else{
            return "notices/new";
        }
    }
    @RequestMapping( value = "create/{flag}", method = RequestMethod.POST)
    public @ResponseBody Message create(@PathVariable("flag")int flag,
            @RequestParam("editor") String editor, @RequestParam("title") String title,
                                        @RequestParam("summer") String summer,
                                        @RequestParam(value = "pic",defaultValue = "") String pic) {
        news.setDate(DateUtil.toDateString(new Date()));
        news.setContent(editor);
        news.setTitle(title);
        news.setPic(pic);
        news.setSummer(summer);
        news.setNewOrNotices(flag);
        newsService.editNews(news);
        message.setMsg(InfoConstant.CREATE_ARTICLE_SUCCESS);
        return message;
    }
    @RequestMapping(value = "delete/{id}",method = RequestMethod.GET)
    public @ResponseBody Message delete(@PathVariable("id")int id){
        newsService.deleteNews(id);
        message.setMsg(InfoConstant.DELETE_ARTICLE_SUCCESS);
        return message;
    }
    @RequestMapping(value = "setLast/{id}/{flag}",method = RequestMethod.GET)
    public @ResponseBody Message setLast(@PathVariable("id")int id,@PathVariable("flag")int flag){
        newsService.setLastNews(id,flag);
        message.setMsg(InfoConstant.UPDATE_NORMAL_SUCCESS);
        return message;
    }
    @RequestMapping(value = "update/{id}/{flag}", method = RequestMethod.GET)
    public String updatePage(@PathVariable("flag")int flag,@PathVariable("id")int id,Model model){
        model.addAttribute("news",newsService.queryNewsById(id));
        if(flag==0){
            return "article/update";
        }else{
            return "notices/update";
        }
}
    @RequestMapping(value = "updateNews/{id}/{flag}",method = RequestMethod.POST)
    public @ResponseBody Message updateNews(@PathVariable("id")int id,@PathVariable("flag")int flag,
                             @RequestParam("title")String title, @RequestParam("summer") String summer,
                             @RequestParam("editor") String editor,@RequestParam(value = "pic",defaultValue = "") String pic){
        //修改的内容
        news.setId(id);
        news.setDate(DateUtil.toDateString(new Date()));
        news.setContent(editor);
        news.setTitle(title);
        news.setSummer(summer);
        news.setNewOrNotices(flag);
        news.setPic(pic);
        //更新
        newsService.updateNews(news);

        message.setMsg(InfoConstant.UPDATE_ARTICLE_SUCCESS);
        return message;

    }
}
